package com.cndongtai.service.net;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.cndongtai.constant.RspCodeConstant;
import com.cndongtai.net.NetController;
import com.cndongtai.net.SocketContainer;
import com.cndongtai.net.message.Message;
import com.cndongtai.po.DtuStatus;
import com.cndongtai.po.LightConfig;
import com.cndongtai.po.LightConfigTemplate;
import com.cndongtai.po.LightFirmware;
import com.cndongtai.util.ByteUtil;
import com.cndongtai.util.DateUtil;
import com.cndongtai.util.SeqUtil;
import com.cndongtai.util.ValidateUtil;
import com.cndongtai.vo.LightBean;
import com.cndongtai.vo.LightStatusBean;

@Service("netService")
public class NetService {
	
	private final int TIMEOUT_SECONDS = 30;
	private final int UP_FIRMWARE_TIMEOUT_MINITES = 10;
	
	
	/**
	 * 修改ID
	 * @param panId
	 * @return
	 */
	public boolean changeLightId(String panId,String lightNum, String panIdNew,String lightNumNew) {
		
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		
		if(container==null) {
			System.out.println(panId + "  没有该设备");
			return false;
		}
		
		byte[] reqMsg = new byte[24];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x12).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xAB).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		System.out.println("req SEQ=>"+seq);
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();
		
		// RTUId
		reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
		reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
		
		
		// PanIdNew
		reqMsg[14] = Long.valueOf(panIdNew.substring(0,2), 16).byteValue();
		reqMsg[15] = Long.valueOf(panIdNew.substring(2), 16).byteValue();
		
		// DeviceIdNew
		reqMsg[16] = Long.valueOf(0x00).byteValue();
		reqMsg[17] = Long.valueOf(0x00).byteValue();
		
		// RTUIdNew
		reqMsg[18] = Long.valueOf(lightNumNew.substring(0,2), 16).byteValue();
		System.out.println(lightNumNew);
		System.out.println(lightNumNew.substring(2));
		reqMsg[19] = Long.valueOf(lightNumNew.substring(2), 16).byteValue();
		
		int crcval = ByteUtil.cal_crc(reqMsg, 20);
		
		reqMsg[20] = Long.valueOf(crcval/256).byteValue();
		reqMsg[21] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[22] = Long.valueOf(0x82).byteValue();
		reqMsg[23] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("读取DTU所管的RTU-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq + "");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return false;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
			container.stopThread();
			NetController.getSocketPool().remove(panId+"_"+lightNum);
			return true;
		}else {
			System.out.println("result=>" + msg.getValue("result"));
			System.out.println("resultmsg=>" + msg.getValue("msg"));
		}
		
		
		return false;
	}
	
	/**
	 * 读取DTU所管的RTU
	 * @param panId
	 * @return
	 */
	public List<LightBean> getLightByPanId(String panId) {
		
		SocketContainer container = NetController.getSocketPool().get(panId);
		
		if(container==null) {
			System.out.println(panId + "  没有该设备");
			return null;
		}
		
		byte[] reqMsg = new byte[19];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x0D).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xA2).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		System.out.println("req SEQ=>"+seq);
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();
		
		// RTU个数
		reqMsg[12] = Long.valueOf(0x00).byteValue();
		
		// RTUId
		reqMsg[13] = Long.valueOf(0x00).byteValue();
		reqMsg[14] = Long.valueOf(0x00).byteValue();
		
		int crcval = ByteUtil.cal_crc(reqMsg, 15);
		
		reqMsg[15] = Long.valueOf(crcval/256).byteValue();
		reqMsg[16] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[17] = Long.valueOf(0x82).byteValue();
		reqMsg[18] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("读取DTU所管的RTU-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq + "");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return null;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
			
			Integer lightSize = Integer.parseInt(msg.getValue("lightSize"));
			System.out.println("lightSize=>" + lightSize);
			if(lightSize>0) {
				List<LightBean> lightList = new ArrayList<LightBean>();
				for(int i=0;i<lightSize;i++) {
					LightBean bean = new LightBean();
					System.out.println("-->" + msg.getValue("lightNum"+i));
					bean.setLightNum(msg.getValue("lightNum"+i));
					lightList.add(bean);
				}
				
				return lightList;
			}
			
		}else {
			System.out.println("result=>" + msg.getValue("result"));
			System.out.println("resultmsg=>" + msg.getValue("msg"));
		}
		
		
		return null;
	}
	
	
	/**
	 * 关灯
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public boolean shutDownLight(String panId, String lightNum) {
		
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		
		if(container==null) {
			return false;
		}
		
		byte[] reqMsg = new byte[31];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x19).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xA8).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();
		
		// RTU DeviceId
		reqMsg[12] = Long.valueOf(0x00).byteValue();
		reqMsg[13] = Long.valueOf(0x00).byteValue();
		
		if(ValidateUtil.empty(lightNum)) {
			reqMsg[12] = Long.valueOf(0x00).byteValue();
			reqMsg[13] = Long.valueOf(0x00).byteValue();
			// 全部
			reqMsg[14] = Long.valueOf(0x00).byteValue();
		}else {
			reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
			reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();

			// 个别
			reqMsg[14] = Long.valueOf(0x01).byteValue();
		}
		
		// R控制时长
		reqMsg[15] = Long.valueOf(0x00).byteValue();
		reqMsg[16] = Long.valueOf(0x00).byteValue();
		
		// 控制电流
		reqMsg[17] = Long.valueOf(0x00).byteValue();
		reqMsg[18] = Long.valueOf(0x00).byteValue();
		
		// 0x00 : 关灯
		reqMsg[19] = Long.valueOf(0x00).byteValue();
		
		// 当前时间
		byte[] dateBytes = ByteUtil.getSysdateBytes();
		reqMsg[20] = dateBytes[0];
		reqMsg[21] = dateBytes[1];
		reqMsg[22] = dateBytes[2];
		reqMsg[23] = dateBytes[3];
		reqMsg[24] = dateBytes[4];
		reqMsg[25] = dateBytes[5];
		reqMsg[26] = dateBytes[6];
		
		int crcval = ByteUtil.cal_crc(reqMsg,27);
		
		reqMsg[27] = Long.valueOf(crcval/256).byteValue();
		reqMsg[28] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[29] = Long.valueOf(0x82).byteValue();
		reqMsg[30] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("关灯-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return false;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
		}else {
			System.out.println("result=>" + msg.getValue("result"));
		}
		
		return true;
	}
	
	
	/**
	 * 开灯
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public boolean turnonLight(String panId, String lightNum, Integer workCurrent, Integer minute) {
		
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		
		if(container==null) {
			return false;
		}
		
		byte[] reqMsg = new byte[31];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x19).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xA8).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();
		
		
		if(ValidateUtil.empty(lightNum)) {
			// RTU DeviceId
			reqMsg[12] = Long.valueOf(0x00).byteValue();
			reqMsg[13] = Long.valueOf(0x00).byteValue();
			// 全部
			reqMsg[14] = Long.valueOf(0x00).byteValue();
		}else {
			reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
			reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
			// 个别
			reqMsg[14] = Long.valueOf(0x01).byteValue();
		}
		
		// R控制时长
		reqMsg[15] = Long.valueOf(minute/256).byteValue();
		reqMsg[16] = Long.valueOf(minute%256).byteValue();
		
		// 控制电流
		reqMsg[17] = Long.valueOf(workCurrent/256).byteValue();
		reqMsg[18] = Long.valueOf(workCurrent%256).byteValue();
		
		// 0x01 : 开灯
		reqMsg[19] = Long.valueOf(0x01).byteValue();
		
		// 当前时间
		byte[] dateBytes = ByteUtil.getSysdateBytes();
		reqMsg[20] = dateBytes[0];
		reqMsg[21] = dateBytes[1];
		reqMsg[22] = dateBytes[2];
		reqMsg[23] = dateBytes[3];
		reqMsg[24] = dateBytes[4];
		reqMsg[25] = dateBytes[5];
		reqMsg[26] = dateBytes[6];
		
		int crcval = ByteUtil.cal_crc(reqMsg, 27);
		reqMsg[27] = Long.valueOf(crcval/256).byteValue();
		reqMsg[28] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[29] = Long.valueOf(0x82).byteValue();
		reqMsg[30] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("开灯-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return false;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
		}else {
			System.out.println("result=>" + msg.getValue("result"));
		}
		
		return true;
	}

	
	/**
	 * 广播教时
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public boolean checkTime(String panId, String lightNum) {
		
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		if(container==null) {
			return false;
		}
		
		byte[] reqMsg = new byte[26];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x14).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xAA).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();
		
		// RTU DeviceId
		reqMsg[12] = Long.valueOf(0x00).byteValue();
		reqMsg[13] = Long.valueOf(0x00).byteValue();
		// 全部
		reqMsg[14] = Long.valueOf(0x00).byteValue();
		
		// 当前时间
		byte[] dateBytes = ByteUtil.getSysdateBytes();
		reqMsg[15] = dateBytes[0];
		reqMsg[16] = dateBytes[1];
		reqMsg[17] = dateBytes[2];
		reqMsg[18] = dateBytes[3];
		reqMsg[19] = dateBytes[4];
		reqMsg[20] = dateBytes[5];
		reqMsg[21] = dateBytes[6];
		
		int crcval = ByteUtil.cal_crc(reqMsg, 22);
		reqMsg[22] = Long.valueOf(crcval/256).byteValue();
		reqMsg[23] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[24] = Long.valueOf(0x82).byteValue();
		reqMsg[25] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("广播教时-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return false;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
		}else {
			System.out.println("result=>" + msg.getValue("result"));
		}
		
		return true;
	}
	
	
	/**
	 * 读取DTU状态数据
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public DtuStatus readDtuStatus(String panId) {
		
		DtuStatus dtuStatus = new DtuStatus();
		
		SocketContainer container = NetController.getSocketPool().get(panId);
		if(container==null) {
			System.out.println(panId + "  该设备不在线");
			return null;
		}
		
		byte[] reqMsg = new byte[23];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x11).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xA1).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();
		
		// 当前时间
		byte[] dateBytes = ByteUtil.getSysdateBytes();
		reqMsg[12] = dateBytes[0];
		reqMsg[13] = dateBytes[1];
		reqMsg[14] = dateBytes[2];
		reqMsg[15] = dateBytes[3];
		reqMsg[16] = dateBytes[4];
		reqMsg[17] = dateBytes[5];
		reqMsg[18] = dateBytes[6];
		
		// CRC16
		int crcval = ByteUtil.cal_crc(reqMsg, 19);
		reqMsg[19] = Long.valueOf(crcval/256).byteValue();
		reqMsg[20] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[21] = Long.valueOf(0x82).byteValue();
		reqMsg[22] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("读取DTU状态数据-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return null;
		}
		
		if("00".equals(msg.getValue("code"))) {
			dtuStatus.setStatus(msg.getValue("dtuStatus"));
			dtuStatus.setTemperature(Integer.valueOf(msg.getValue("temperature")));
			dtuStatus.setVol(Integer.valueOf(msg.getValue("vol"))/10.0);
		}else {
			return null;
		}
		
		return dtuStatus;
	}
	
	/**
	 * 读取RTU状态数据
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public LightStatusBean readRtuStatus(String panId, String lightNum) {
		LightStatusBean lightStatus = new LightStatusBean();
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		if(container==null) {
			return null;
		}
		
		byte[] reqMsg = new byte[19];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x0D).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xA6).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();
		
		// RTU DeviceId
		if(ValidateUtil.empty(lightNum)) {
			reqMsg[12] = Long.valueOf(0x00).byteValue();
			reqMsg[13] = Long.valueOf(0x00).byteValue();
			// 全部
			reqMsg[14] = Long.valueOf(0x00).byteValue();
		}else {
			reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
			reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
			// 个别
			reqMsg[14] = Long.valueOf(0x01).byteValue();
		}
		
		int crc16 = ByteUtil.cal_crc(reqMsg, 15);
		reqMsg[15] = Long.valueOf(crc16/256).byteValue();
		reqMsg[16] = Long.valueOf(crc16%256).byteValue();
		
		reqMsg[17] = Long.valueOf(0x82).byteValue();
		reqMsg[18] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("读取RTU状态数据-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return null;
		}
		
		if("00".equals(msg.getValue("code"))) {
			lightStatus.setStatusCode(msg.getValue("statusCode"));
			if("10".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("空闲");
			}else if("20".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("充电");
			}else if("30".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("放电-灯灭");
			}else if("31".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("放电-灯亮");
			}else if("40".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("放电结束");
			}else if("A1".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("欠压");
			}else if("A2".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("开路");
			}else if("A3".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("过载");
			}else if("A4".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("峰值配置错误");
			}else if("A5".equals(lightStatus.getStatusCode())) {
				lightStatus.setStatusType("电池接反");
			}
			
			lightStatus.setTemperature(Integer.valueOf(msg.getValue("temperature"))/10.0 + "");
			lightStatus.setSolarPanelVol(Integer.valueOf(msg.getValue("solarPanelVol"))/10.0 + "");
			lightStatus.setBatteryVol(Integer.valueOf(msg.getValue("batteryVol"))/10.0 + "");
			lightStatus.setChargeCurrent(Integer.valueOf(msg.getValue("chargeCurrent")));
			lightStatus.setWorkCurrent(Integer.valueOf(msg.getValue("workCurrent")));
			lightStatus.setWorkVol(Integer.valueOf(msg.getValue("workVol"))/10.0 + "");
			lightStatus.setChargeCount(Integer.valueOf(msg.getValue("chargeCount"))/10.0 + "");
			lightStatus.setWorkCount(Integer.valueOf(msg.getValue("workCount"))/10.0 + "");
			
			System.out.println("result=>" + msg.getValue("result"));
		}else {
			System.out.println("result=>" + msg.getValue("result"));
			return null;
		}
		
		return lightStatus;
	}
	
	/**
	 * 读取RTU配置数据
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public LightConfig readRtuConfig(String panId, String lightNum) {
		LightConfig lightConfig = new LightConfig();
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		if(container==null) {
			return null;
		}
		
		byte[] reqMsg = new byte[18];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x0C).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xA5).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();

		// RTU DeviceId
		reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
		reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
		
		int crcval = ByteUtil.cal_crc(reqMsg, 14);
		reqMsg[14] = Long.valueOf(crcval/256).byteValue();
		reqMsg[15] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[16] = Long.valueOf(0x82).byteValue();
		reqMsg[17] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("读取RTU配置数据-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return null;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
			lightConfig.setLightType(msg.getValue("lightType"));
			lightConfig.setSerialNum(msg.getValue("serialNum"));
			lightConfig.setProductTime(DateUtil.timeFormatBCD(msg.getValue("productTime")));
			lightConfig.setControlModel(msg.getValue("controlModel"));
			lightConfig.setTurnonVol(Integer.valueOf(msg.getValue("turnonVol")));
			lightConfig.setTurnonTime(msg.getValue("turnonTime"));
			lightConfig.setShutdownTime(msg.getValue("shutdownTime"));
			lightConfig.setWorkModel(msg.getValue("workModel"));
			lightConfig.setTopVol(Integer.parseInt(msg.getValue("topVol")));
			lightConfig.setLowVol(Integer.parseInt(msg.getValue("lowVol")));
			lightConfig.setExitVol(Integer.parseInt(msg.getValue("exitVol")));
			lightConfig.setLowStartVol(Integer.parseInt(msg.getValue("lowStartVol")));
			lightConfig.setLowEndVol(Integer.parseInt(msg.getValue("lowEndVol")));
			lightConfig.setMaxWorkCurrent(Integer.parseInt(msg.getValue("maxWorkCurrent")));
			lightConfig.setTime1(Integer.parseInt(msg.getValue("time1")));
			lightConfig.setWorkCurrent1(Integer.parseInt(msg.getValue("workCurrent1")));
			lightConfig.setTime2(Integer.parseInt(msg.getValue("time2")));
			lightConfig.setWorkCurrent2(Integer.parseInt(msg.getValue("workCurrent2")));
			lightConfig.setTime3(Integer.parseInt(msg.getValue("time3")));
			lightConfig.setWorkCurrent3(Integer.parseInt(msg.getValue("workCurrent3")));
			lightConfig.setTime4(Integer.parseInt(msg.getValue("time4")));
			lightConfig.setWorkCurrent4(Integer.parseInt(msg.getValue("workCurrent4")));
		}else {
			System.out.println("result=>" + msg.getValue("result"));
			return null;
		}
		
		return lightConfig;
	}
	
	/**
	 * RTU配置
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public String rtuConfig(String panId, String lightNum, LightConfigTemplate template) {
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		if(container==null) {
			return RspCodeConstant.ERROR_OFF_LINE;
		}
		
		byte[] reqMsg = new byte[50];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x2C).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xA3).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();
				
		// RTU DeviceId
		reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
		reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
		
		// 控制模式
		reqMsg[14] = Long.valueOf(Long.parseLong(template.getControlModel(),16)).byteValue();
		
		
		// 开灯电压
		if(template.getTurnonVol()!=null) {
			reqMsg[15] = Long.valueOf(template.getTurnonVol()).byteValue();
		}else {
			reqMsg[15] = Long.valueOf(0x00).byteValue();
		}
		
		String turnonTime = template.getTurnonTime();
		if(ValidateUtil.notEmpty(turnonTime)) {
			String[] turnonTimeArr = turnonTime.split(":");
			// 开灯时间
			reqMsg[16] = Long.valueOf(turnonTimeArr[0]).byteValue();
			reqMsg[17] = Long.valueOf(turnonTimeArr[1]).byteValue();
		}else {
			reqMsg[16] = Long.valueOf(0x00).byteValue();
			reqMsg[17] = Long.valueOf(0x00).byteValue();
		}
		
		
		String shutdownTime = template.getShutdownTime();
		if(ValidateUtil.notEmpty(shutdownTime)) {
			String[] shutdownTimeArr = shutdownTime.split(":");
			// 关灯时间
			reqMsg[18] = Long.valueOf(shutdownTimeArr[0]).byteValue();
			reqMsg[19] = Long.valueOf(shutdownTimeArr[1]).byteValue();
		}else {
			// 关灯时间
			reqMsg[18] = Long.valueOf(0x00).byteValue();
			reqMsg[19] = Long.valueOf(0x00).byteValue();
		}
		
		// 工作模式
		reqMsg[20] = Long.valueOf(template.getWorkModel()).byteValue();
		
		// 峰值保护14
		reqMsg[21] = Long.valueOf(template.getTopVol()/256).byteValue();
		reqMsg[22] = Long.valueOf(template.getTopVol()%256).byteValue();
		
		// 欠压保护11
		reqMsg[23] = Long.valueOf(template.getLowVol()/256).byteValue();
		reqMsg[24] = Long.valueOf(template.getLowVol()%256).byteValue();
		
		// 退出保护13
		reqMsg[25] = Long.valueOf(template.getExitVol()/256).byteValue();
		reqMsg[26] = Long.valueOf(template.getExitVol()%256).byteValue();
		
		// 降功率起始电压12
		reqMsg[27] = Long.valueOf(template.getLowStartVol()/256).byteValue();
		reqMsg[28] = Long.valueOf(template.getLowStartVol()%256).byteValue();
		
		// 降功率终止电压
		reqMsg[29] = Long.valueOf(template.getLowEndVol()/256).byteValue();
		reqMsg[30] = Long.valueOf(template.getLowEndVol()%256).byteValue();
		
		// 一段时长
		reqMsg[31] = Long.valueOf(template.getTime1()/256).byteValue();
		reqMsg[32] = Long.valueOf(template.getTime1()%256).byteValue();

		// 一段电流
		reqMsg[33] = Long.valueOf(template.getWorkCurrent1()).byteValue();
		
		// 二段时长
		reqMsg[34] = Long.valueOf(template.getTime2()/256).byteValue();
		reqMsg[35] = Long.valueOf(template.getTime2()%256).byteValue();

		// 二段电流
		reqMsg[36] = Long.valueOf(template.getWorkCurrent2()).byteValue();
		
		// 三段时长
		reqMsg[37] = Long.valueOf(template.getTime3()/256).byteValue();
		reqMsg[38] = Long.valueOf(template.getTime3()%256).byteValue();

		// 三段电流
		reqMsg[39] = Long.valueOf(template.getWorkCurrent3()).byteValue();
		
		// 四段时长
		reqMsg[40] = Long.valueOf(template.getTime4()/256).byteValue();
		reqMsg[41] = Long.valueOf(template.getTime4()%256).byteValue();

		// 四段电流
		reqMsg[42] = Long.valueOf(template.getWorkCurrent4()).byteValue();
		
		// 五段时长
		reqMsg[43] = Long.valueOf(0L).byteValue();

		// 五段电流
		reqMsg[44] = Long.valueOf(template.getMaxWorkCurrent()/256).byteValue();
		reqMsg[45] = Long.valueOf(template.getMaxWorkCurrent()%256).byteValue();
		
		int crcval = ByteUtil.cal_crc(reqMsg, 46);
		reqMsg[46] = Long.valueOf(crcval/256).byteValue();
		reqMsg[47] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[48] = Long.valueOf(0x82).byteValue();
		reqMsg[49] = Long.valueOf(0xB5).byteValue();
		
		// xx
		
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<reqMsg.length;i++) {
			sb.append(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(reqMsg[i])).toUpperCase())).append(" ");
		}
		
		System.out.println("===>" + sb.toString());
		
		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("RTU配置-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			return RspCodeConstant.ERROR_TIME_OUT;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
			return RspCodeConstant.SUCCESS;
		}else {
			System.out.println("result=>" + msg.getValue("result"));
			return msg.getValue("result");
		}
		
	}
	
	/**
	 * 查询DTU中存储的RTU状态数据
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public boolean readRtuStatusInDtu(String panId, String lightNum, Integer days) {
		
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		
		if(container==null) {
			return false;
		}
		
		byte[] reqMsg = new byte[19];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x0D).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xAB).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();

		// RTU DeviceId
		reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
		reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
		
		reqMsg[14] = Long.valueOf(days).byteValue();
		
		int crcval = ByteUtil.cal_crc(reqMsg, 15);
		reqMsg[15] = Long.valueOf(crcval/256).byteValue();
		reqMsg[16] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[17] = Long.valueOf(0x82).byteValue();
		reqMsg[18] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("查询DTU中存储的RTU状态数据-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return false;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
		}else {
			System.out.println("result=>" + msg.getValue("result"));
		}
		
		return true;
	}
	
	/**
	 * 查询DTU中存储的RTU电量数据
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public boolean readRtuCurrentInDtu(String panId, String lightNum, Integer days, boolean isCharge) {

		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		
		if(container==null) {
			return false;
		}
		
		byte[] reqMsg = new byte[20];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x0E).byteValue();
		
		reqMsg[4] = Long.valueOf(0x28).byteValue();
		reqMsg[5] = Long.valueOf(0xAC).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();

		// RTU DeviceId
		reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
		reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
		
		reqMsg[14] = Long.valueOf(days).byteValue();
		
		if(isCharge) {
			reqMsg[15] = Long.valueOf(0x00).byteValue();
		}else {
			reqMsg[15] = Long.valueOf(0x01).byteValue();
		}
		
		int crcval = ByteUtil.cal_crc(reqMsg, 16);
		reqMsg[16] = Long.valueOf(crcval/256).byteValue();
		reqMsg[17] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[18] = Long.valueOf(0x82).byteValue();
		reqMsg[19] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("查询DTU中存储的RTU电量数据-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return false;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
		}else {
			System.out.println("result=>" + msg.getValue("result"));
		}
		
		return true;
	}
	
	/**
	 * 发起固件升级
	 * @param panId
	 * @param lightNum
	 * @return
	 */
	public String upFirmware(String panId, String lightNum, String uploadType,
			String protocalVersion,String hardwareVersion,String sotfwareVersion,
			String url,int firmwareSize,int firmwareCRC) {
		
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}

		if(container==null) {
			return "0001";
		}
		
		// URL地址
		byte[] byteArr = url.getBytes();
		int length = byteArr.length;
				
		byte[] reqMsg = new byte[33+length];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf((27+length)/256).byteValue();
		reqMsg[3] = Long.valueOf((27+length)%256).byteValue();
		
		reqMsg[4] = Long.valueOf(0x80).byteValue();
		reqMsg[5] = Long.valueOf(0xA1).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();

		// RTU DeviceId
		reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
		reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
		
		reqMsg[14] = Long.valueOf(uploadType, 16).byteValue();
		
		reqMsg[15] = Long.valueOf(protocalVersion, 16).byteValue();
		reqMsg[16] = Long.valueOf(hardwareVersion, 16).byteValue();
		reqMsg[17] = Long.valueOf(sotfwareVersion, 16).byteValue();
		
		// 升级包大小
		int sizeTop = firmwareSize/(256*256);
		reqMsg[18] = Long.valueOf(sizeTop/256).byteValue();
		reqMsg[19] = Long.valueOf(sizeTop%256).byteValue();
		int sizeDown = firmwareSize%(256*256);
		reqMsg[20] = Long.valueOf(sizeDown/256).byteValue();
		reqMsg[21] = Long.valueOf(sizeDown%256).byteValue();
		
		// 升级包校验
		reqMsg[22] = Long.valueOf(firmwareCRC/256).byteValue();
		reqMsg[23] = Long.valueOf(firmwareCRC%256).byteValue();
		
		// 连接升级服务器时限
		reqMsg[24] = Long.valueOf(UP_FIRMWARE_TIMEOUT_MINITES).byteValue();
		
		//URL地址长度
		int top = length/(256*256);
		reqMsg[25] = Long.valueOf(top/256).byteValue();
		reqMsg[26] = Long.valueOf(top%256).byteValue();
		int down = length%(256*256);
		reqMsg[27] = Long.valueOf(down/256).byteValue();
		reqMsg[28] = Long.valueOf(down%256).byteValue();
		// URL
		for(int h=0;h<length;h++) {
			reqMsg[29+h] = byteArr[h];
		}

		int crcval = ByteUtil.cal_crc(reqMsg, 29+length);
		reqMsg[29+length] = Long.valueOf(crcval/256).byteValue();
		reqMsg[30+length] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[31+length] = Long.valueOf(0x82).byteValue();
		reqMsg[32+length] = Long.valueOf(0xB5).byteValue();
		
		
		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("发起固件升级-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return "0002";
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
		}else {
			System.out.println("result=>" + msg.getValue("result"));
		}
		
		return msg.getValue("result");
	}
	
	/**
	 * 查询设备固件信息
	 * @param panId
	 * @param lightNum
	 * @param uploadType
	 * @return
	 */
	public LightFirmware readDeviceFirmwareInfo(String panId, String lightNum, String uploadType) {
		LightFirmware lightFirmware = null;
		
		SocketContainer container = NetController.getSocketPool().get(panId+"_"+lightNum);
		if(container==null) {
			container = NetController.getSocketPool().get(panId);
		}
		if(container==null) {
			return lightFirmware;
		}
		
		byte[] reqMsg = new byte[20];
		
		reqMsg[0] = Long.valueOf(0xA2).byteValue();
		reqMsg[1] = Long.valueOf(0x5B).byteValue();
		
		reqMsg[2] = Long.valueOf(0x00).byteValue();
		reqMsg[3] = Long.valueOf(0x0D).byteValue();
		
		reqMsg[4] = Long.valueOf(0x80).byteValue();
		reqMsg[5] = Long.valueOf(0xA0).byteValue();
		
		Integer seq = SeqUtil.getNextSeq();
		reqMsg[6] = Long.valueOf(seq/256).byteValue();
		reqMsg[7] = Long.valueOf(seq%256).byteValue();
		
		// PanId
		reqMsg[8] = Long.valueOf(panId.substring(0,2), 16).byteValue();
		reqMsg[9] = Long.valueOf(panId.substring(2), 16).byteValue();
		
		// DTU DeviceId
		reqMsg[10] = Long.valueOf(0x00).byteValue();
		reqMsg[11] = Long.valueOf(0x00).byteValue();

		// RTU DeviceId
		reqMsg[12] = Long.valueOf(lightNum.substring(0,2), 16).byteValue();
		reqMsg[13] = Long.valueOf(lightNum.substring(2), 16).byteValue();
		
		reqMsg[14] = Long.valueOf(uploadType, 16).byteValue();
		
		int crcval = ByteUtil.cal_crc(reqMsg, 15);
		reqMsg[15] = Long.valueOf(crcval/256).byteValue();
		reqMsg[16] = Long.valueOf(crcval%256).byteValue();
		
		reqMsg[17] = Long.valueOf(0x82).byteValue();
		reqMsg[18] = Long.valueOf(0xB5).byteValue();

		Message msg = null;
		if(container.send(reqMsg)) {
			System.out.println("查询设备固件信息-发送成功");
			// 发送成功
			for(int i=0;i<TIMEOUT_SECONDS;i++) {
				msg = NetController.getMessage(seq+"");
				if(msg == null) {
					try {
						Thread.sleep(1000*1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					NetController.removeMessage(seq+"");
					break;
				}
			}
		}
		
		// 处理响应数据
		if(msg==null) {
			System.out.println("请求超时");
			return lightFirmware;
		}
		
		if("00".equals(msg.getValue("code"))) {
			System.out.println("result=>" + msg.getValue("result"));
			lightFirmware = new LightFirmware();
			lightFirmware.setProtocalVersion(msg.getValue("protocalVersion"));
			lightFirmware.setHardwareVersion(msg.getValue("hardwareVersion"));
			lightFirmware.setFirmwareVersion(msg.getValue("firmwareVersion"));
			lightFirmware.setFirmwareTime(DateUtil.timeFormatBCD(msg.getValue("firmwareTime")));
			lightFirmware.setImei(msg.getValue("imei"));
			lightFirmware.setIccid(msg.getValue("iccid"));
			
		}else {
			System.out.println("result=>" + msg.getValue("result"));
		}
		
		return lightFirmware;
	}

}
